package chpt05;

import java.time.Duration;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Period;
import java.time.ZoneOffset;
import java.time.chrono.IsoChronology;
import java.time.temporal.ChronoUnit;

public class PeriodAndDurationTest {

	public static void main(String[] args) {
		// Duration用来表示基于机器时间的时间间隔（秒、纳秒），常和Instant使用。
		// 演示Duration
		
		Duration duration = Duration.ofSeconds(5); // 获取5s的时间间隔实例
		LocalDateTime now = LocalDateTime.of(2023, 2, 27, 10, 0, 0);
		LocalDateTime plus = now.plus(duration); // 2023-02-27T10:00:05
		System.out.println("now = "+now);
		System.out.println("5秒后 = "+plus);
		Instant epoch = Instant.ofEpochSecond(10);  // 获取纪元时间的时间点
		Instant after = epoch.plus(duration);      // 5
		System.out.println("epoch = "+epoch.getEpochSecond());
		System.out.println("after = "+after.getEpochSecond());
		
		// Periond用来表示基于日期时间的时间间隔（年月日），常与LocalDateTime使用
		 LocalDate firstDay = LocalDate.of(2023, 9, 1); // 开学日
		 LocalDate endDay = firstDay.plusWeeks(20); // 学期结束日
		 Period period = Period.between(firstDay, endDay);
		 long d = ChronoUnit.DAYS.between(firstDay, endDay);
		 System.out.println("开学日: "+ firstDay);
		 System.out.println("闭学日: "+ endDay);
		 int months = period.getMonths();
		 int days = period.getDays();
		 System.out.println("学期持续" + months +"月, "+ days +"天, 共"+d+"天");
		 Period period2 = Period.ofWeeks(18); // 18周的日期间隔 
		 LocalDate endDay2 = firstDay.plus(period2);
		 System.out.println("如果一学期有18周。闭学日: "+endDay2);
		 
		 
	}


}
